package ru.spb.osll.myfit.client.log;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RemoteLogger {
  private static LoggingServiceAsync loggingService = GWT
      .create(LoggingService.class);

  public static int FATAL = 50000;
  public static int ERROR = 40000;
  public static int WARN = 30000;
  public static int INFO = 20000;
  public static int DEBUG = 10000;
  public static int TRACE = 5000;

  private static void logging(int level, final String message) {
    loggingService.logClientIssue(level, message, new AsyncCallback<Object>() {
      public void onSuccess(Object result) {
        result = message;
      }

      public void onFailure(Throwable caught) {
      }
    });
  }

  public static void log(final int level, String message) {
    if (level == ERROR || (level == FATAL)) {
      GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
        public void onUncaughtException(Throwable t) {
          logging(level, "Uncaught Exception: " + t.getMessage());
        }
      });
    } 
   logging(level, message);
  }
  
  public static void info(String message){
    logging(INFO, message);
  }
  
  public static void debug(String message){
    logging(DEBUG, message);
  }
  
  public static void error(String message){
    log(ERROR, message);
  }
  
  public static void warn(String message){
    logging(WARN, message);
  }
  
  public static void fatal(String message){
    log(FATAL, message);
  }
  
//  public static void info(String message, Throwable t){
//    logging(INFO, message);
//  }
//  
//  public static void debag(String message, Throwable t){
//    logging(DEBUG, message);
//  }
//  
//  public static void error(String message, Throwable t){
//    logging(ERROR, message);
//  }
//  
//  public static void warn(String message, Throwable t){
//    logging(WARN, message);
//  }
//  
//  public static void fatal(String message, Throwable t){
//    logging(FATAL, message);
//  }
  
}